/*
 * CFile1.c
 *
 * Created: 28/12/2013 07.51.24
 *  Author: cicci_000
 */ 
#include <string.h>
#include "digital_io.h"
#include "bool.h"
#include "simpleEventHandler.h"

#include "PMARData.h"

#define MAX_KEYPAD_KEYS 6

static unsigned char Keypad_Values[MAX_KEYPAD_KEYS]; 
static unsigned char Keypad_PreviousValue[MAX_KEYPAD_KEYS];
static unsigned char KeyPressed=false;

//*******************************************************************
//                           Keypad_Init
//*******************************************************************
void Keypad_Init (void)
{
	memset(Keypad_Values,0,MAX_KEYPAD_KEYS*sizeof(char));
	memset(Keypad_PreviousValue,0,MAX_KEYPAD_KEYS*sizeof(char));
}

//*******************************************************************
//                           Keypad_scan
//*******************************************************************
void Keypad_scan (char * Digital_Input_Data)
{   
	unsigned char i;
	
	for (i=0;i<MAX_KEYPAD_KEYS;i++)
	{
		Keypad_Values[i]=Digital_Input_Data[i];
	}
	
	if (Keypad_Values[TASTO_START]!=Keypad_PreviousValue[TASTO_START])
	{
		if (Keypad_Values[TASTO_START]) SEQ_AddEvent(Tasto_Start_Pressed);
		else SEQ_AddEvent(Tasto_Start_Released);
	}
	if (Keypad_Values[TASTO_STOP]!=Keypad_PreviousValue[TASTO_STOP])
	{
		if (Keypad_Values[TASTO_STOP]) SEQ_AddEvent(Tasto_Stop_Pressed);
		else SEQ_AddEvent(Tasto_Stop_Released);
	}
	if (Keypad_Values[TASTO_RESET]!=Keypad_PreviousValue[TASTO_RESET])
	{
		if (Keypad_Values[TASTO_RESET]) SEQ_AddEvent(Tasto_Reset_Pressed);
		else SEQ_AddEvent(Tasto_Reset_Released);
	}
	if (Keypad_Values[TASTO_PIU]!=Keypad_PreviousValue[TASTO_PIU])
	{
		if (Keypad_Values[TASTO_PIU]) SEQ_AddEvent(Tasto_Piu_Pressed);
		else SEQ_AddEvent(Tasto_Piu_Released);
	}
	if (Keypad_Values[TASTO_MENO]!=Keypad_PreviousValue[TASTO_MENO])
	{
		if (Keypad_Values[TASTO_MENO]) SEQ_AddEvent(Tasto_Meno_Pressed);
		else SEQ_AddEvent(Tasto_Meno_Released);
	}	
	if (Keypad_Values[TASTO_ZERO]!=Keypad_PreviousValue[TASTO_MENO])
	{
		if (Keypad_Values[TASTO_ZERO]) SEQ_AddEvent(Tasto_Zero_Pressed);
		else SEQ_AddEvent(Tasto_Zero_Released);
	}	
			//if (Keypad_Values[i]!=Keypad_PreviousValue[i]) {
				//KeyPressed=true;
			//}
			
	
	for (i=0;i<MAX_KEYPAD_KEYS;i++)
	{
		Keypad_PreviousValue[i]=Keypad_Values[i];
	}
}


//*******************************************************************
//                           Keypad_read
//*******************************************************************
unsigned char Keypad_read (char * Digital_Input_Data)
{
		unsigned char i;
		unsigned char KeyPad_Keys=0;
		
		for (i=0;i<MAX_KEYPAD_KEYS;i++)
		{
			KeyPad_Keys|=(Digital_Input_Data[i]<<i)&(~(1<<i));
		}
		return KeyPad_Keys;
}

//*******************************************************************
//                           Keypad_HasBeenPressed
//*******************************************************************
unsigned char Keypad_HasBeenPressed (void)
{   unsigned char TempStatus;
	TempStatus=KeyPressed;
	KeyPressed=false;
	return TempStatus;
}

